package com.envision.utils;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.protocol.HTTP;

import java.io.IOException;

/**
 * <strong>Title : PhoneMsgUtils.java<br>
 * </strong> <strong>Description : 短信工具类</strong><br>
 * <strong>Create on : 2018年03月07日<br>
 * <p>
 *
 * @author karrychang<br>
 * @version <strong>1.0.0</strong><br>
 * <br>
 * <strong>修改历史:</strong><br>
 * 修改人		修改日期		修改描述<br>
 * -------------------------------------------<br>
 * <br>
 * <br>
 */
public class PhoneMsgUtils {

    private String url;
    /**
    *************************************************************************
     * PhoneMsgUtils.java——sendPhoneMsgToUserName<br>
     * Author: karrychang<br>
     * Date: 2018/3/7<br>
     * Description:用户名发送短信<br>
     * Used in：<br>
     * @param users
     * @param msg
     * @return
     *************************************************************************
     */
    public int sendPhoneMsgToUserName(String [] users,String msg){
        CloseableHttpClient httpClient = HttpClients.createDefault();
        String str=this.url+"/api/sms";
        HttpPost httpPost=new HttpPost(str);
        httpPost.setHeader("Content-Type","application/json");
        String str1="{\"users\":[\""+users[0]+"\"],\"msg\":\""+msg+"\"}";
        StringEntity entity1 = new StringEntity(str1, HTTP.UTF_8);
        httpPost.setEntity(entity1);
        entity1.setContentType("application/json");
        int i=201;
        try {
            CloseableHttpResponse response= httpClient.execute(httpPost);
            i=response.getStatusLine().getStatusCode();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return i;
    }
    /**
    *************************************************************************
     * PhoneMsgUtils.java——sendPhoneMsgToPhoneNumber<br>
     * Author: karrychang<br>
     * Date: 2018/3/7<br>
     * Description:手机号发送短信<br>
     * Used in：<br>
     * @param users
     * @param msg
     * @return
     *************************************************************************
     */
    public int sendPhoneMsgToPhoneNumber(String[] users,String msg){
        CloseableHttpClient httpClient = HttpClients.createDefault();
        String str=this.url+"/api/message";
        HttpPost httpPost=new HttpPost(str);
        httpPost.setHeader("Content-Type","application/json");
        String str1="{\"users\":[\""+users[0]+"\"],\"msg\":\""+msg+"\"}";
        StringEntity entity1 = new StringEntity(str1, HTTP.UTF_8);
        httpPost.setEntity(entity1);
        entity1.setContentType("application/json");
        int i=201;
        try {
            CloseableHttpResponse response= httpClient.execute(httpPost);
            i=response.getStatusLine().getStatusCode();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return i;
    }

    /**
    *************************************************************************
     * PhoneMsgUtils.java——sendMail<br>
     * Author: karrychang<br>
     * Date: 2018/3/7<br>
     * Description:发送文字邮件<br>
     * Used in：<br>
     * @param mail
     * @param subject
     * @param content
     * @return
     *************************************************************************
     */
    public int sendMail(String mail,String subject,String content){
        CloseableHttpClient httpClient = HttpClients.createDefault();
        String str=this.url+"/api/sendmail";
        HttpPost httpPost=new HttpPost(str);
        httpPost.setHeader("Content-Type","application/json");
        String str1="{\"to\":\""+mail+"\",\"subject\":\""+subject+"\",\"content\":\""+content+"\"}";
        StringEntity entity1 = new StringEntity(str1, HTTP.UTF_8);
        httpPost.setEntity(entity1);
        entity1.setContentType("application/json");
        int i=201;
        try {
            CloseableHttpResponse response= httpClient.execute(httpPost);
            i=response.getStatusLine().getStatusCode();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return i;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }
}
